package com.yzr.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author yzr
 * @date 2021/08/06 14:16
 * @description 时间格式化工具类
 **/
public class DateUtil {

    private static final List<Integer> LASTTERM = Arrays.asList(2, 3, 4, 5, 6, 7, 8);

    private static final List<Integer> NEXTTERM = Arrays.asList(9, 10, 11, 12);

    public enum Format {
        /**
         * 格式化格式
         */
        YYYY_MM_DD("yyyy-MM-dd"),
        MM_DD_HH_MM("MM-dd HH:mm"),
        YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm"),
        YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss");

        private final String format;

        Format(String format) {
            this.format = format;
        }

        public String value() {
            return this.format;
        }
    }

    /**
     * 将LocalDateTime类型格式化为字符串
     *
     * @param dateTime 日期时间
     * @param pattern  格式化类型
     * @return
     */
    public static String formatDate(LocalDateTime dateTime, String pattern) {
        String format = "";
        if (dateTime == null) {
            return format;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            format = formatter.format(dateTime);
        } catch (Exception e) {
            return format;
        }
        return format;
    }


    /**
     * 字符串转localDateTime
     *
     * @param str
     * @return
     */
    public static LocalDateTime strToDate(String str, String format) {
        if (format.equals(Format.YYYY_MM_DD.value())) {
            str = str + " 00:00:00";
            format = Format.YYYY_MM_DD_HH_MM_SS.value();
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(str, df);
    }


    /**
     * 获取startDate日期后month月的日期
     *
     * @param startDate 开始日期
     * @param month     几个月后
     * @return Date
     */
    public static Date getMonthDate(Date startDate, int month) {
        LocalDateTime localDateTime = startDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime().plusMonths(month);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据传入时间获取开始时间
     *
     * @param time 开始日期
     * @return LocalDateTime
     */
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    /**
     * 获取昨天的月份 yyyyMM
     *
     * @return String
     */
    public static String getYesterday() {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(yesterday);
    }

    /**
     * 获取今天的月份 yyyyMM
     *
     * @return String
     */
    public static String getToday() {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, 0);
        Date yesterday = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(yesterday);
    }

    /**
     * 获取今天所在的学期月份的集合
     *
     * @return String
     */
    public static List<String> getTermMMs() {
        List<String> termMMs;
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, 0);
        Date today01 = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String format = sdf.format(today01);
        Integer mm = Integer.valueOf(format.substring(4));
        Integer yyyy = Integer.valueOf(format.substring(0, 4));
        if (LASTTERM.contains(mm)) {
            termMMs = Arrays.asList(yyyy + "02", yyyy + "03", yyyy + "04", yyyy + "05", yyyy + "06", yyyy + "07", yyyy + "08");
        } else if (NEXTTERM.contains(mm)) {
            termMMs = Arrays.asList(yyyy + "09", yyyy + "10", yyyy + "11", yyyy + "12", yyyy + 1 + "01");
        } else {
            termMMs = Arrays.asList(yyyy - 1 + "09", yyyy - 1 + "10", yyyy - 1 + "11", yyyy - 1 + "12", yyyy + "01");
        }
        return termMMs;
    }

    public static Date getFewDays(Integer days, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }
}
